<?php
/*
	Resistor divider generator, by Leah Rowe
	Written as a web application, for hosting on a PHP-enabled HTTP server.
	Uses GET data for user input. Output displayed as text, in HTML.

	Given specified input voltage in mV and intended output voltage in mV use a
	list of known standard resistor values, show to the engineer which resistor
	values are ideal when designing a resistor divider circuit.

	Copyright (C) 2020  Leah Rowe
	Contact email: info@minifree.org
	Twitter: https://twitter.com/n4of7/
	Website: https://retrofreedom.com/

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
$timeStart=(float)microtime();
if (false)
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit (6);
}

//sleep(1);

$arrE24 = array
(
	1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
	3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1
);

$arrE48 = array
(
	1.00, 1.05, 1.10, 1.15, 1.21, 1.27, 1.33, 1.40, 1.47, 1.54, 1.62, 1.69,
	1.78, 1.87, 1.96, 2.05, 2.15, 2.26, 2.37, 2.49, 2.61, 2.74, 2.87, 3.01,
	3.16, 3.32, 3.48, 3.65, 3.83, 4.02, 4.22, 4.42, 4.64, 4.87, 5.11, 5.36,
	5.62, 5.90, 6.19, 6.49, 6.81, 7.15, 7.50, 7.87, 8.25, 8.66, 9.09, 9.53
);
$arrE96 = array
(
	1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30,
	1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74,
	1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32,
	2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09,
	3.16, 3.24, 3.32, 3.40, 3.48, 3.57, 3.65, 3.74, 3.83, 3.92, 4.02, 4.12,
	4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49,
	5.62, 5.76, 5.90, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32,
	7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76
);
$arrE192 = array
(
	1.00, 1.01, 1.02, 1.04, 1.05, 1.06, 1.07, 1.09, 1.10, 1.11, 1.13, 1.14,
	1.15, 1.17, 1.18, 1.20, 1.21, 1.23, 1.24, 1.26, 1.27, 1.29, 1.30, 1.32,
	1.33, 1.35, 1.37, 1.38, 1.40, 1.42, 1.43, 1.45, 1.47, 1.49, 1.50, 1.52,
	1.54, 1.56, 1.58, 1.60, 1.62, 1.64, 1.65, 1.67, 1.69, 1.72, 1.74, 1.76,
	1.78, 1.80, 1.82, 1.84, 1.87, 1.89, 1.91, 1.93, 1.96, 1.98, 2.00, 2.03,
	2.05, 2.08, 2.10, 2.13, 2.15, 2.18, 2.21, 2.23, 2.26, 2.29, 2.32, 2.34,
	2.37, 2.40, 2.43, 2.46, 2.49, 2.52, 2.55, 2.58, 2.61, 2.64, 2.67, 2.71,
	2.74, 2.77, 2.80, 2.84, 2.87, 2.91, 2.94, 2.98, 3.01, 3.05, 3.09, 3.12,
	3.16, 3.20, 3.24, 3.28, 3.32, 3.36, 3.40, 3.44, 3.48, 3.52, 3.57, 3.61,
	3.65, 3.70, 3.74, 3.79, 3.83, 3.88, 3.92, 3.97, 4.02, 4.07, 4.12, 4.17,
	4.22, 4.27, 4.32, 4.37, 4.42, 4.48, 4.53, 4.59, 4.64, 4.70, 4.75, 4.81,
	4.87, 4.93, 4.99, 5.05, 5.11, 5.17, 5.23, 5.30, 5.36, 5.42, 5.49, 5.56,
	5.62, 5.69, 5.76, 5.83, 5.90, 5.97, 6.04, 6.12, 6.19, 6.26, 6.34, 6.42,
	6.49, 6.57, 6.65, 6.73, 6.81, 6.90, 6.98, 7.06, 7.15, 7.23, 7.32, 7.41,
	7.50, 7.59, 7.68, 7.77, 7.87, 7.96, 8.06, 8.16, 8.25, 8.35, 8.45, 8.56,
	8.66, 8.76, 8.87, 8.98, 9.09, 9.20, 9.31, 9.42, 9.53, 9.65, 9.76, 9.88
);

$mVin = isset($_GET["vin"])
	? (float)(int)floatval($_GET['vin'])
	: (float)0;

$mVout_target = isset($_GET["vtarget"])
	? (float)(int)floatval($_GET['vtarget'])
	: (float)0;

$strRTarget = isset($_GET["rtarget"])
	? (string)$_GET["rtarget"]
	: "";
$strRTarget = (float)floatval($strRTarget) > 0
	? (string)(float)floatval($strRTarget)
	: "";

$tolerance = isset($_GET["tolerance"])
	? (float)floatval($_GET["tolerance"])
	: (float)0.1;
$tolerance = $tolerance>=0 && $tolerance<100
	? $tolerance
	: (float)0.1;

$order= isset($_GET["order"])
	? (string)$_GET["order"]
	: "ascending";
$order = $order != "ascending" && $order != "descending"
	? "ascending"
	: $order;

$arrE=array();
$eSeries=isset($_GET["e"])
	? (int)floatval($_GET["e"])
	: 24;
switch($eSeries) {
	case 24: $arrE=$arrE24; break;
	case 48: $arrE=$arrE48; break;
	case 96: $arrE=$arrE96; break;
	case 192:$arrE=$arrE192; break;
	default:
		$arrE=$arrE24;
		$eSeries=24;
		break; }

$arrEFull=$arrE;
for ($power=1; $power <= 6; $power++)
	for ($arrE_index = 0; $arrE_index < count($arrE); $arrE_index++)
		array_push(
			$arrEFull,
			$fResistor = floatval($arrE[$arrE_index]) * (10**$power)
		);

function strResistanceReadable($fResistor)
{
	$fResistor=(float)$fResistor;

	for ($i = 0; $i < 3; $i++)
		if ($fResistor>=1000**$i && $fResistor<1000**($i+1))
			return
				(string)round(($fResistor / (1000**$i)),2)
				.(string)array("Ω","K","M")[$i];

	return (string)round($fResistor,2)."Ω";
}

function fReadableFloat($i)
{
	$i=(float)$i;
	$negative=false; if($negative=$i<0) $i -= 2*$i;

	for ($power=1; $power<12; $power++)
		if ($i >= 10/(10**$power) && $i < 10/(10**($power-1)))
			return round($negative ? $i-(2*$i) : $i, $power);

	return round($negative ? $i-(2*$i) : $i, 1);
}

function rFind($r, &$arrEFull)
{
	$r=(float)$r;
	if(!is_array($arrEFull)) return -1;

	$eCount = count($arrEFull);
	$eBaseCount = $eCount / 7;
	$eHalfCount = $eBaseCount / 2;

	// These are the 3 if's that hold the universe together. DO NOT REMOVE!
	if ($r<0.95 || $r>(1.05*$arrEFull[$eCount-1])) return -1;
	if ($r>=0.95 && $r<=1) return 1;
	if ($r<=(1.05*$arrEFull[$eCount-1]) && $r>=$arrEFull[$eCount-1])
		return $eCount-1;

	$power=0; $n=$r; for (;$n>=10; ++$power) $n /= 10;
	$rPos = ($power * $eBaseCount) + $eHalfCount;

	while ($eHalfCount > 1) {
		if ($eHalfCount == 2) ++$rPos;
		$eHalfCount -= (int)($eHalfCount / 2);
		if ($r < $arrEFull[$rPos-1]) { $rPos -= $eHalfCount; continue; }
		if ($r > $arrEFull[$rPos]) { $rPos += $eHalfCount; continue; }
		break; }

	return $rPos;
}

function rAddonSeriesGet($r1, $arrR, $diffLeft, &$arrEFull)
{
	$r1 = (float)$r1;
	$r1_ideal = $diffLeft = (float)$diffLeft;

	if(!is_array($arrR)) $arrR=array();
	if(!is_array($arrEFull)) return $arrR;
	if ($diffLeft<=0 || $r1 <= 0) return $arrR;

	$rPos = rFind($r1_ideal, $arrEFull);

	if ($rPos<0) return $arrR;
	if ($rPos==0) { array_push($arrR,array($r1,(float)1)); return $arrR; }
	if ($rPos == count($arrEFull)-1) {
		array_push($arrR, array($r1, $arrEFull[count($arrEFull)-2]));
		return $arrR; }

	$diffLeft = $r1_ideal - $arrEFull[$rPos-1];
	$diffRight = $arrEFull[$rPos] - $r1_ideal;

	if ($diffLeft == $diffRight) {
		array_push($arrR, array($r1, $arrEFull[$rPos-1]));
		array_push($arrR, array($r1, $arrEFull[$rPos])); }
	else if ($diffLeft < $diffRight)
		array_push($arrR, array($r1, $arrEFull[$rPos-1]));
	else if ($diffLeft > $diffRight)
		array_push($arrR, array($r1, $arrEFull[$rPos]));

	return $arrR;
}

function rAddonParallelGet($r1, $arrR, $diffRight, &$arrEFull)
{
	$r1=(float)$r1;
	$diffRight=(float)$diffRight;

	if(!is_array($arrR)) $arrR=array();
	if(!is_array($arrEFull)) return $arrR;

	$arrR1 = $arrR;
	$r1_ideal = $r1 - $diffRight;

	$rAddIdeal = ($r1 * $r1_ideal) / ($r1 - $r1_ideal);
	$rAddIndex = rFind($rAddIdeal, $arrEFull);

	if ($rAddIndex == -1)
		return $arrR1;
	if ($rAddIndex == count($arrEFull)-1) {
		array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
		return $arrR1; }
	if ($rAddIndex == 0) {
		array_push($arrR1, array($r1,-(float)1));
		return $arrR1; }

	$diffLeft = $rAddIdeal - $arrEFull[$rAddIndex-1];
	$diffRight = $arrEFull[$rAddIndex] - $rAddIdeal;

	if ($diffLeft == $diffRight) {
		array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
		array_push($arrR1, array($r1,-$arrEFull[$rAddIndex])); }
	else if ($diffLeft < $diffRight)
		array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
	else if ($diffLeft > $diffRight)
		array_push($arrR1, array($r1,-$arrEFull[$rAddIndex]));

	return $arrR1;
}

function r1Expand($r2,$mVin,$mVout_target,&$arrEFull)
{
	$r2=(float)$r2;
	$mVin=(float)$mVin;
	$mVout_target=(float)$mVout_target;
	if(!is_array($arrEFull)) return array();

	$r1_ideal = $r2 * (($mVin - $mVout_target) / $mVout_target);
	$rPos = rFind($r1_ideal, $arrEFull);

	$arrR1 = array();

	if ($rPos < 0 || $rPos == count($arrEFull)-1) return array();
	if ($rPos == 0) return array($arrR1,array((float)1));
	if ($rPos == count($arrEFull)-1)
		return array($arrR1,array($arrEFull[count($arrEFull)-2]));

	$diffLeft = $r1_ideal - $arrEFull[$rPos-1];
	$diffRight = $arrEFull[$rPos] - $r1_ideal;

	if ($diffLeft == $diffRight) {
		array_push($arrR1,array($arrEFull[$rPos-1]));
		$arrR1 = $diffLeft > 0
			? rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull)
			: $arrR1;
		$arrR1 = $diffRight > 0
			? rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight, $arrEFull)
			: $arrR1;
		array_push($arrR1,array($arrEFull[$rPos])); }

	else if ($diffLeft < $diffRight) {
		array_push($arrR1,array($arrEFull[$rPos-1]));
		$arrR1 = $diffLeft > 0
			? rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull)
			: $arrR1; 
		$arrR1 =
			rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight,$arrEFull);}

	else if ($diffLeft > $diffRight) {
		$arrR1 =
			rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull); 
		$arrR1 = $diffRight > 0
			? rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight, $arrEFull)
			: $arrR1;
		array_push($arrR1,array($arrEFull[$rPos])); }

	return $arrR1;
}

function arrGetResults($mVin, $mVout_target, &$arrEFull, $tolerance, $rtarget)
{
	$timeStart = microtime();
	if(!is_array($arrEFull)) return array();
	if (!is_float($tolerance)) $tolerance=(float)0.1;
	$tolerance=(float)$tolerance;
	if($tolerance<0||$tolerance>=100) $tolerance=(float)0.1;
	$rtarget = (float)floatval($rtarget);
	if ($rtarget<0) $rtarget=0;

	$multiplier = (float)(1+($tolerance/100));

	$mVin=(float)$mVin;
	$mVout_target=(float)$mVout_target;

	$arrResults=array();
	$eCount = count($arrEFull);

	if ($mVin <= 0 || $mVout_target <= 0) return array();
	else if ($mVin == $mVout_target) return array();
	else if ($mVout_target > $mVin) return array();

	for ($r2index=0; $r2index<$eCount; $r2index++) {
		$r2 = $arrEFull[$r2index];
		$arrR1 = r1Expand($r2,$mVin,$mVout_target,$arrEFull);

		if ($rtarget != 0) {
			$r1_ideal = $r2 * (($mVin - $mVout_target) / $mVout_target);
			$rOut_ideal = ($r1_ideal * $r2) / ($r1_ideal + $r2);

			if ($rOut_ideal < ($rtarget * 0.925) || $rOut_ideal > ($rtarget * 1.075))
				continue; }

		for ($matchIndex=0; $matchIndex<count($arrR1); $matchIndex++) {
			$r1 = (float)0;
			$arrR1part = $arrR1[$matchIndex];

			for ($i=0; $i<count($arrR1part); $i++)
				$r1 = $arrR1part[$i] >= 0
					? $r1 + $arrR1part[$i]
					: ($r1 * (-$arrR1part[$i])) / ($r1 + (-$arrR1part[$i]));

			$arrmVout = array();
			$arrOhmOut = array();
			$arrmAout = array();
			$arrErrOut = array();
			
			$arrR = array(
				array($r1 * $multiplier, $r2 * ((float)2 - $multiplier)),
				array($r1, $r2),
				array($r1 * ((float)2 - $multiplier), $r2 * $multiplier));	

			for ($r=0; $r < count($arrR); $r++) {
				$realR1 = $arrR[$r][0];
				$realR2 = $arrR[$r][1];

				$mVout = ($mVin * $realR2) / ($realR2 + $realR1);
				array_push($arrmVout, $mVout);
				array_push($arrOhmOut, ($realR1 * $realR2) / ($realR2 + $realR1));
				array_push($arrmAout, $mVin / ($realR1 + $realR2));
				array_push($arrErrOut,
					100 * (($mVout - $mVout_target) / $mVout_target)); }

			array_push(
				$arrResults, array(
					$arrR1part,$r2,$arrmVout,$arrOhmOut,$arrmAout,$arrErrOut)); } }

	$timeEnd = microtime();

	array_push($arrResults,
		array(
			$mVin,
			(float)(1000*((float)$timeEnd-(float)$timeStart)),
			$tolerance,
			$rtarget,
			$mVout_target
		));

	return $arrResults;
}

function arrGetHumanReadableResults(&$arrResults)
{
	if (!is_array($arrResults)) return array();
	if (count($arrResults)==0) return array();

	$arrReadable =
		array(
			array(
				array("R1","(high)"),
				array("R2","(low)"),
				array("Output(mV)", "mVin*(r2/(r1+r2))"),
				array("Output(Ω)", "(r1*r2)/(r1+r2)"),
				array("Output(mA)", "(mVin/(r1+r2))"),
				array("Error%", "(V)")));
	
	for ($i=0; $i<count($arrResults)-1; $i++) {
	
		$arrR1 = $arrResults[$i][0];
		$series = $parallel = false;

		if (count($arrR1)>1) {
			if ($arrR1[1]<0) {
				$parallel=true;
				$arrR1[1]-=2*$arrR1[1]; }
			else $series=true; }

		$r1 = "";

		for ($j=0; $j<count($arrR1) && $j<2; $j++) {
			$arrR1[$j]=strResistanceReadable($arrR1[$j]);
			$r1 .= ($j>0?" + ":"") . $arrR1[$j]; }

		$r2 = strResistanceReadable($arrResults[$i][1]);
		
		$arrmVout = array(
			$arrResults[$i][2][0], $arrResults[$i][2][1], $arrResults[$i][2][2]);
		$arrOhmOut = array(
			$arrResults[$i][3][0], $arrResults[$i][3][1], $arrResults[$i][3][2]);
		$arrmAout = array(
			$arrResults[$i][4][0], $arrResults[$i][4][1], $arrResults[$i][4][2]);
		$arrErrOut = array(
			$arrResults[$i][5][0], $arrResults[$i][5][1], $arrResults[$i][5][2]);
		
		for ($arrIndex=0; $arrIndex<count($arrmVout); $arrIndex++) {
			$arrmVout[$arrIndex] = (string)fReadableFloat($arrmVout[$arrIndex]);
			$arrOhmOut[$arrIndex] = strResistanceReadable($arrOhmOut[$arrIndex]);
			$arrmAout[$arrIndex] = (string)fReadableFloat($arrmAout[$arrIndex]);
			$arrErrOut[$arrIndex] = (string)fReadableFloat($arrErrOut[$arrIndex]); }
		
		array_push($arrReadable,
			array (
				array($r1,($series?"(series)":"").($parallel?"(parallel)":"")),
				array($r2,""),
				array(
					$arrmVout[1]."mV",
					$arrmVout[0]!=$arrmVout[1]
						? "(".$arrmVout[0]." to ".$arrmVout[2]."mV)"
						: ""),
				array($arrOhmOut[1],
					$arrOhmOut[0]!=$arrOhmOut[1]
						? "(".$arrOhmOut[0]." to ".$arrOhmOut[2].")"
						: ""),
				array($arrmAout[1]."mA",
					$arrmAout[0]!=$arrmAout[1]
						? "(".$arrmAout[0]." to ".$arrmAout[2]."mA)"
						: ""),
				array($arrErrOut[1]."%",
					$arrErrOut[0]!=$arrErrOut[1]
						? "(".$arrErrOut[0]." to ".$arrErrOut[2]."%)"
						: "")
				)); }

	array_push($arrReadable,
		array(
			(string)fReadableFloat($arrResults[count($arrResults)-1][0])."mV",
			(string)fReadableFloat($arrResults[count($arrResults)-1][1])." milliseconds",
			(string)fReadableFloat($arrResults[count($arrResults)-1][2])."%",
			(string)strResistanceReadable($arrResults[count($arrResults)-1][3]),
			(string)fReadableFloat($arrResults[count($arrResults)-1][4])."mV"
			)
		);

	return $arrReadable;
}

function strHtmlResults(&$arrReadable, $ascending, $eSeries)
{
	$ascending = is_bool($ascending) ? $ascending : true;
	if (!is_array($arrReadable))
		return "";
	if (count($arrReadable)==0)
		return "";

	ob_start();
	$strROut = $arrReadable[count($arrReadable)-1][3];
	if ($strROut == "0Ω") $strROut="";
	else $strROut = ", output resistance target ".$strROut;

	?>
	<p style="text-align:center;">
		<a href="app.php">Click here to reset the form</a>
	</p>
	<h1 id="computations">Results</h1>
	<p>
		<?php echo (string)(count($arrReadable)-2); ?> results
		computed in <?php echo $arrReadable[count($arrReadable)-1][1]; ?>,
		displayed in <?php echo $ascending?"ascending":"descending"; ?> order with
		<?php echo $arrReadable[count($arrReadable)-1][0]; ?> input voltage and
		<?php echo $arrReadable[count($arrReadable)-1][4]; ?> target.<br/>
		Resistor tolerance
		<?php echo $arrReadable[count($arrReadable)-1][2]; ?><?php echo $strROut; ?>.
		E<?php echo (string)$eSeries; ?> resistor values used.
	</p>
	<?php

	?><table class="results"><tr><?php

	for ($i=0; $i<count($arrReadable[0]) && $i>=0; $i++) { ?>
		<th>
		<?php echo $arrReadable[0][$i][0]
			."<br/><small>".$arrReadable[0][$i][1]."</small>";
		?></th>
		<?php }
		?></tr><?php

	for ($i=$ascending?1:count($arrReadable)-1; $i<count($arrReadable)-1 && $i>=1;) {
		?><tr><?php
		for ($j=0; $j<count($arrReadable[$i]); $j++) { ?>
			<td class="results">
			<?php echo
				$arrReadable[$i][$j][0]."<br/>"
				."<small>".$arrReadable[$i][$j][1]."</small>";
			?></td> <?php }
			?></tr><?php 
			$i+=$ascending?1:-1;}

	?></table><?php

	$strHtml = ob_get_clean();
	return $strHtml;
}

?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="generator" content="pandoc">
  <meta name="viewport" 
	content="width=device-width, initial-scale=0.5, user-scalable=yes">
  <title>Resistor divider generator</title>
  <link rel="stylesheet" href="global.css">
  <link rel="stylesheet" href="headercenter.css">
  <?php include_once "mobile.php"; ?>
</head>
<body>
<header>
<p>
<img src="diagram.svg" alt="Resistor divider circuit diagram" id="logo" />
<img 
	src="diagram.svg"
	alt="Resistor divider circuit diagram" id="logo" class="logoleft" />
</p>
<h1 class="title">Resistor divider generator</h1>
</header>

<p>
	A resistor divider (see diagram) divides a larger voltage into a smaller
	one.<br/>
	<strong>mVout = (mVin * R2) / (R1 + R2)</strong>
</p>
<p>
	This program generates a table of resistor pairs for your circuit.
	<a href="../">Click here to return to retrofreedom.com homepage</a>
</p>

<h2>Enter your data here:</h2>
<form method="get" action="app.php#computations">
<table>
	<tr>
		<th>Input(mV)</th>
		<th>Output(mV)</th>
		<th>Output(Ω)<br/><small>(optional)</small></th>
		<th>Tolerance% (R1/R2)</th>
		<th>E series</th>
		<th><label for="order">Order</label></th>
	</tr>
	<tr>
	<td class="input">
		<input 
			type="text"
			onfocus="this.value=''"
			name="vin"
			value="<?php if ($mVin!=0) printf("%dmV",$mVin); ?>"
		/>
	</td>
	<td class="input">
		<input
			type="text"
			onfocus="this.value=''"
			name="vtarget"
			value="<?php if ($mVout_target!=0) printf("%dmV",$mVout_target); ?>"
		/>
	</td>
	<td class="input">
		<input
			type="text"
			onfocus="this.value=''"
			name="rtarget"
			value="<?php printf("%s%s",$strRTarget, $strRTarget!=""?"Ω":""); ?>"
		/>
	</td>
	<td class="input">
		<input
			type="text"
			onfocus="this.value=''"
			name="tolerance"
			value="<?php printf("%s%%",(string)$tolerance); ?>"
		/>
	</td>
	<td class="input">
		<select id="e" name="e">
			<option value="24"<?php if ($eSeries=="24") printf(" selected"); ?>>
				E24
			</option>
			<option value="48"<?php if ($eSeries=="48") printf(" selected"); ?>>
				E48
			</option>
			<option value="96"<?php if ($eSeries=="96") printf(" selected"); ?>>
				E96
			</option>
			<option value="192"<?php if ($eSeries=="192") printf(" selected"); ?>>
				E192
			</option>
		</select>
	</td>
	<td class="input">
		<select id="order" name="order">
			<option 
				value="ascending"
				<?php if ($order=="ascending") printf(" selected"); ?>>
				Ascending
			</option>
			<option
				value="descending"
				<?php if ($order=="descending") printf(" selected"); ?>>
				Descending
			</option>
		</select>
	</td>
	</tr>
</table>
<input class="redbutton" type="submit" value="Click here for results" />
</form>

<?php
	$arrResults =
		arrGetResults(
			$mVin, $mVout_target, $arrEFull, $tolerance, $strRTarget);

	$arrReadable = arrGetHumanReadableResults($arrResults);
	$strHtml = strHtmlResults($arrReadable, $order == "ascending", $eSeries);
	echo $strHtml;
?>

<h2>About this program</h2>

<p>
	Sometimes, you want to precisely tune a signal and to do that, you might
	use a resistor divider. Other resistor divider calculators exist, but
	they usually give you the most ideal values. In the real world,
	resistors come in fixed sizes. With this web application, you can give
	a voltage and a desired output voltage, where you will then be given a
	precisely calculated list of resistor combinations to use, for actual
	real world resistor values, in your divider circuit. To my knowledge,
	no other such program existed, simple though it may be. My name is
	Leah Rowe, and you can learn more about me from my personal website
	at <a href="https://vimuser.org/">https//vimuser.org/</a>.
</p>

<p>
	You can learn more about voltage dividers by reading
	<a href="https://en.wikipedia.org/wiki/Voltage_divider">the following Web 
	page on WikiPedia</a>.
</p>

<p>
	This program is <a href="https://www.gnu.org/philosophy/free-sw.html">Free
	Software</a>, released under the terms of the GNU Affero General Public
	License, either version 3 of the license as published by the
	<a href="https://www.fsf.org/">Free Software Foundation</a>, or (at your
	option) any later version as published by the
	<a href="https://www.fsf.org/">Free Software Foundation</a>. A copy of this
	license is included; see <a href="COPYING.txt">COPYING.txt</a>.
	Source code is available via
	<a href="https://notabug.org/vimuser/resistordivider/">https://notabug.org/vimuser/resistordivider/</a>
</p>

<p>
	The resistor divider diagram is released under Creative Commons Zero
	license version 1.0, as per this wikipedia page where it was found:
	<a href="https://en.wikipedia.org/wiki/Voltage_divider#/media/File:Resistive_divider2.svg">https://en.wikipedia.org/wiki/Voltage_divider#/media/File:Resistive_divider2.svg</a>
</p>

</body>
</html>
<?php
?>
